<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>排序算法之快速排序</title>
  </head>
  <body>
    <script type="text/javascript">
      function quickSort(arr) {
        if (arr.length < 2) {
          return arr;
        }
        const pivot = arr[0]; // 基准值
        const left = [], right = [];
        for (let i = 1; i < arr.length; i++) {
          if (arr[i] < pivot) {
            left.push(arr[i]);
          } else {
            right.push(arr[i]);
          }
        }
        return quickSort(left).concat(pivot, quickSort(right));
      }

      const arr = [10, 6, 3, 4, 8, 1, 2];
      console.log(quickSort(arr));
    </script>
  </body>
</html>
